<?php


namespace app\mobile\model;

use think\Model;
use think\Session;

class NewsModel extends Model
{
    protected $table = 'tp_news';

    public function getTypeNameAttr()
    {
        $typeInfo = TypeModel::get($this->typeId);
        return !empty($typeInfo) ? $typeInfo->typeName : '';
    }

    public function getList($typeId, $count = 8, $isIndex = 1)
    {
        if($typeId==0){
          return self::where(['state' => 1, 'isIndex' => $isIndex])
            ->order('sort asc')
            ->paginate($count);
        } else {
          return self::where(['state' => 1, 'isIndex' => $isIndex, 'typeId' => $typeId])
            ->order('sort asc')
            ->paginate($count);
        }

    }

    public function getNewList($count = 10)
    {
        return self::where([ 'state' => 1])
            ->order('id desc')
            ->paginate($count);
    }

    public function getIsNewsList($count = 10)
    {
        return self::where([ 'state' => 1, 'isTui' => 1])
            ->order('sort asc')
            ->paginate($count);
    }

    public function getInfo($id)
    {
        return self::where([ 'id' => $id])->order('id asc')->limit(1)->find();
    }

    public function getPre($id, $typeId)
    {
        return self::where(['id' => ['>', $id], 'typeId' => $typeId])
            ->order('id asc')->limit(1)->find();
    }

    public function getNext($id, $typeId)
    {
        return self::where(['id' => ['<', $id], 'typeId' => $typeId])
            ->order('id asc')->limit(1)->find();
    }

    public function getListAll($condition = [], $count = 8)
    {
        $data = array_merge($condition);
        return self::where($data)->order('sort desc')->paginate($count);
    }
}